<?php
/*
 *                        _oo0oo_
 *                       o8888888o
 *                       88" . "88
 *                       (| -_- |)
 *                       0\  =  /0
 *                     ___/`---'\___
 *                   .' \\|     |// '.
 *                  / \\|||  :  |||// \
 *                 / _||||| -:- |||||- \
 *                |   | \\\  - /// |   |
 *                | \_|  ''\---/''  |_/ |
 *                \  .-\__  '-'  ___/-. /
 *              ___'. .'  /--.--\  `. .'___
 *           ."" '<  `.___\_<|>_/___.' >' "".
 *          | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *          \  \ `_.   \_ __\ /__ _/   .-` /  /
 *      =====`-.____`.___ \_____/___.-`___.-'=====
 *                        `=---='
 * 
 * 
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * 
 *            佛祖保佑       永不宕机     永无BUG
 */
/*
 * 搜索页
 * @Author: htong
 * @Date: 2021-03-02 18:16:55
 * @LastEditors: htong
 * @LastEditTime: 2021-03-02 18:39:06
 * @Description: file content
 * @FilePath: \gongchengqiaojob\Application\Home\Controller\PcSearchController.class.php
 */
namespace Home\Controller;
use Common\Controller\PcApiController;
use Common\ORG\PublicFunction;
use Common\ORG\WxFunction;
use Think\Db;

class PcSearchApiController extends PcApiController{
	
	public function _initialize() {
 //csdcdsfdsfdsf
        $getsecret  = $_SERVER['HTTP_APPSECRET'];
        //取head标签内容
        if(!preg_match('/^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/', $getsecret) && ACTION_NAME<>'getUserInfo'){
            $this->ajaxReturn(1, 'appsecret格式错误');
        }

        if($this->appsecret <> $getsecret && ACTION_NAME<>'getUserInfo'){
            $this->ajaxReturn(1, 'appsecret错误');
        }
        parent::_initialize();
    }
    /**
     * 搜索入口
     */
	public function entrance(){ 

        $user =  $this->userinfo;
        if($user['utype'] !=1) {
            $this->ajaxReturn(1, L('auth_null'));//用户不存在
        }
        $data = $this->get_left_useritem();

        $data['experiencelist'] = $this->get_experience_list();//工作经验
        $data['educationlist'] = $this->get_education_list();//获取学历要求
        $data['agelist'] = $this->get_age_list();//年龄
        $data['sexlist'] = [['id'=>1,'value'=>'男'],['id'=>2,'value'=>'女']];//性别
        $data['wagelist'] = $this->get_wage_list();//期望薪资
        $data['languagelist'] = $this->get_language_list();//语言
        $data['citylist'] = $this->get_city_parent_list();//城市
        $data['jobswlist'] = $this->get_jobsw_list();//职位
        $data['trade'] = $this->get_major_list();//工程
        $data['settrlist'] = [['id'=>3,'value'=>'3天内'],['id'=>7,'value'=>'7天内'],['id'=>15,'value'=>'15天内'],['id'=>30,'value'=>'30天内']];//更新时间
        $this->ajaxReturn(0, 'ok', $data);
    }
    /**
     * 搜索
     */
    public function Search(){
        $user =  $this->userinfo;
        if($user['utype'] !=1) {
            $this->ajaxReturn(1, L('auth_null'));//用户不存在
        }
        $keyword = I('keyword',''); //关键字
        $typesearch = I('typesearch',''); //搜索类型
        $jobs = I('jobs',''); //职位类别
        $jobsarr = explode(",", $jobs);  
        $major =  I('major',''); //工程类别
        $majorarr = explode(",", $major);  
        $experience =  I('experience',''); //工作经验
        $liveprovince =  I('liveprovince',''); //居住地省
        $livecity =  I('livecity',''); //居住地市
        $livearea =  I('livearea',''); //居住地区
        $expectprovince =  I('expectprovince',''); //期望工作地点省
        $expectcity =  I('expectcity',''); //期望工作地点市
        $education =  I('education',''); //学历要求
        $age =  I('age',''); //年龄
        $sex =  I('sex',''); //性别
        $wage =  I('wage',''); //薪资
        $language = I('language',''); //语言
        $settr = I('settr',''); //更新时间
        $pagesize = I('pagesize',10);//每页条数
        $page = I('page',1);//页数

        $where = ['s.display'=>['in',[0,1]],'s.audit'=>1,'m.utype'=>2];
        $where2 = [];
       
        //人才搜索
        if($keyword && $typesearch == 'personnel' ){
            $where2['s.title|s.fullname'] = ['like','%'.$keyword.'%'];
        }
       
        //全文
        if($keyword && $typesearch == 'full' ){
            $where2['s.specialty'] = ['like','%'.$keyword.'%'];
        }
        //职位名称
        if($keyword && $typesearch == 'position' ){
            $where2['s.intention_jobs'] = ['like','%'.$keyword.'%'];
        }
        //工程类别
        if($major){
            foreach($majorarr as $mav){
                $majors = M('category_major')->field('categoryname')->where(['id'=>$mav])->find();
                $majorswhere[]=['like','%'.$majors['categoryname'].'%'];
            }
            $majorswhere[]='or';
            $where2['s.trade_cn'] =$majorswhere;
        }



        //工作经验
        if($experience){
            
            switch($experience){
                case 74:
                    $where2['s.first_work_date'] = ['eq',''];
                    break;  
                case 75:
                    $data1 = date('Y-m-d',strtotime('-1 year'));
                    $where2['s.first_work_date'] = ['egt',$data1];
                    break;
                case 76:
                    $data1 = date('Y-m-d',strtotime('-1 year'));
                    $data3 = date('Y-m-d',strtotime('-3 year'));
                    $where2['s.first_work_date'] = [['lt',$data1],['egt',$data3]];
                    break;
                case 77:
                    $data3 = date('Y-m-d',strtotime('-3 year'));
                    $data5 = date('Y-m-d',strtotime('-5 year'));
                    $where2['s.first_work_date'] = [['lt',$data3],['egt',$data5]];
                    break;
                case 78:
                    $data5 = date('Y-m-d',strtotime('-5 year'));
                    $data10 = date('Y-m-d',strtotime('-10 year'));
                    $where2['s.first_work_date'] = [['lt',$data5],['egt',$data10]];
                    break;
                case 79:
                    $data10 = date('Y-m-d',strtotime('-10 year'));
                    $where2['s.first_work_date'] = ['lt',$data10];
                    break;
                default:
            }
        }
        //居住地
        if($liveprovince || $livecity || $livearea){
            
                if($liveprovince && !$livecity && !$livearea){
                    $where2['s.residence'] = ['like',$liveprovince.'.%'];
                }
                if($liveprovince && $livecity && !$livearea){
                    $where2['s.residence'] = ['like',$liveprovince.'.'.$livecity.'.%'];
                }
                if($liveprovince && $livecity && $livearea){
                    $where2['s.residence'] = ['eq',$liveprovince.'.'.$livecity.'.'.$livearea];
                }
        }
        //期望工作地点
        if($expectprovince || $expectcity ){
            
            if($expectprovince && !$expectcity  ){
                $where2['s.key_full'] = ['like','%city'.$expectprovince.'%'];
            }
            if($expectprovince && $expectcity ){
                $where2['s.key_full'] = ['like','%city'.$expectprovince.'_'.$expectcity.'%'];
            }
        }
        //职位类别
        if($jobs){
            foreach($jobsarr as  $vjob){
                $parentjobsid = M('category_jobs')->field('parentid')->where(['id'=>$vjob])->find();
                $parentjobsid1 = M('category_jobs')->field('parentid')->where(['id'=>$parentjobsid['parentid']])->find();
                if($parentjobsid1['parentid'] > 0){
                    $jobss = $parentjobsid1['parentid'].'.'.$parentjobsid['parentid'].'.'.$vjob;
                }else{
                    if($parentjobsid['parentid'] > 0){
                        $jobss = '%'.$parentjobsid['parentid'].'.'.$vjob.'%';
                    }else{
                        $jobss = '%'.$vjob.'.0%';
                    }
                }
                $jobswhere[]=['like',$jobss];
            }
           
            $jobswhere[]='or';
         
            $where2['s.intention_jobs_id|s.jobs_id'] = $jobswhere;
        }
       
        //学历要求
        if($education ){
            $where2['re.education'] = ['egt',$education];
        }
        //年龄
        if($age){
            switch($age){
                case 296:
                    $starttime = date('Y-m-d',strtotime('-16 year'));
                    $endtime = date('Y-m-d',strtotime('-20 year'));
                    $where2['s.birthdate'] = [['elt',$starttime],['egt',$endtime]];
                    break;  
                case 297:
                    $starttime = date('Y-m-d',strtotime('-21 year'));
                    $endtime = date('Y-m-d',strtotime('-30 year'));
                    $where2['s.birthdate'] = [['elt',$starttime],['egt',$endtime]];
                    break;  
                case 298:
                    $starttime = date('Y-m-d',strtotime('-31 year'));
                    $endtime = date('Y-m-d',strtotime('-40 year'));
                    $where2['s.birthdate'] = [['elt',$starttime],['egt',$endtime]];
                    break;  
                case 299:
                    $starttime = date('Y-m-d',strtotime('-41 year'));
                    $endtime = date('Y-m-d',strtotime('-50 year'));
                    $where2['s.birthdate'] = [['elt',$starttime],['egt',$endtime]];
                    break;  
                case 300:
                    $starttime = date('Y-m-d',strtotime('-50 year'));
                    $where2['s.birthdate'] = ['lt',$starttime];
                    break;  
                default:
            }
        }
        //性别
        if($sex ){
            $where2['s.sex'] = ['eq',$sex];
        }
        //期望薪资
        if($wage ){
            $where2['s.wage'] = ['eq',$wage];
        }
        //语言
        if($language ){
            $where2['rl.language'] = ['eq',$language];
        }
        //语言
        if($settr){
            $starttime = strtotime('-'.$settr.' day');
            $where2['s.refreshtime'] = ['egt',$starttime];
        }
        $count = M('Resume s')
                    ->join(C('DB_PREFIX').'members as m on m.uid = s.uid')  
                    ->join(C('DB_PREFIX').'resume_education as re on re.pid = s.id')  
                    ->join(C('DB_PREFIX').'resume_language as rl on rl.pid = s.id')          
                    ->where($where)
                    ->where($where2)
                    ->count();
    //  print_r( M('Resume s')->getLastSql());die;
        //人才简历
        $resumelist = M('Resume s')
                    ->field('s.fullname,s.id,s.uid,s.sex,s.birthdate,s.education_cn,s.major_cn,s.wage_cn,s.intention_jobs,s.current_cn,s.jobs_id,s.trade_cn,s.residence,s.refreshtime')
                    ->join(C('DB_PREFIX').'members as m on m.uid = s.uid')  
                    ->join(C('DB_PREFIX').'resume_education as re on re.pid = s.id')  
                    ->join(C('DB_PREFIX').'resume_language as rl on rl.pid = s.id')          
                    ->where($where)
                    ->where($where2)
                    ->page($page,$pagesize)
                    ->group('s.id')
                    ->order('refreshtime desc')
                    ->select();
       
        foreach($resumelist as $k=>$v){
            $jobs       = explode(',', $v['jobs_id']);
            $jobsName   = [];
            foreach($jobs as $val){
                $job_t      = explode('.', $val);
                $jid        = !empty($job_t[1]) ? $job_t[1] : $job_t[0];
                  !empty(D('CategoryJobs')->getJobsName($jid)) ? $jobsName[]=D('CategoryJobs')->getJobsName($jid) : '';
            }
            $speciality = M('resume_education')->field('speciality')->where(['pid'=>$v['id']])->order('education desc')->find();
            $resumelist[$k]['major_cn'] =  $speciality['speciality'];
            $resumelist[$k]['jobs_sn'] =  implode(",",$jobsName);
            $resumelist[$k]['residence'] =D('CategoryDistrict')->getCityName($v['residence']);
            $resumelist[$k]['age'] =get_age($v['birthdate']);
            $resumelist[$k]['refreshtime_cn']= daterange(time(),$v['refreshtime']);
            $resumelist[$k]['photosrc']=avatar($v['uid']);
            //判断是否下载
            $is_down=$this->check_down_resume($v['uid'],C('visitor.uid'));
            $resumelist[$k]['is_down']=$is_down;
            if(!$resumelist[$k]['is_down']){
              $resumelist[$k]['fullname'] = contact_hide($v['fullname'], -2);
            }
        }
        $data = $this->get_left_useritem();
        $data['count'] = $count;
        $data['resumelist'] = $resumelist;
        $this->ajaxReturn(0, 'ok',$data );
    }

}
?>